var innerAudioContext = wx.createInnerAudioContext();
const app = getApp();
var util = require('../../../utils/util.js');
var storage = require('../../../utils/storage.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isOpen: true, //播放开关
    starttime: "00:00", //正在播放时长
    duration: "00:00", //总时长
    offset: 0,
    max: 0,
    src: "",
    objectid: "",
    content: "",
    coverObjectid: ''
      
  },
  listenerButtonPlay: function() {
    var that = this;
    innerAudioContext.src = that.data.src;
    innerAudioContext.play();
    // innerAudioContext.onPlay((res) => {
      innerAudioContext.onTimeUpdate(() => {
        setTimeout(() => {
          innerAudioContext.duration         
            that.setData({
              starttime: util.formatSeconds(innerAudioContext.currentTime.toFixed(0)),
              duration: util.formatSeconds(innerAudioContext.duration.toFixed(0)),
              max: innerAudioContext.duration.toFixed(0),
              offset: innerAudioContext.currentTime.toFixed(0)
            })
        }, 1000)
      })
    // })
    //监听音频结束，切换图标
    innerAudioContext.onEnded((res) =>{
      that.setData({
        isOpen: true,
        starttime: "00:00"
      })
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    that.setData({
      isOpen: false
    })
  },
    //暂停播放
  listenerButtonPause() {
    var that = this;
    innerAudioContext.pause();
    that.setData({
      isOpen: true
    })
  },

    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) { 
      //屏蔽右上角分享功能
    wx.hideShareMenu();
    var objectid = options.objectid;
    var temp = options.temp;
    var activityId = options.activityId;
    var cover = options.cover;
    
    innerAudioContext.src = temp;//初始化音频播放组件
    var content = storage.get('content');
    console.log("audio - content: " + content);
    this.setData({
      content: content,
      objectid: objectid,
      src: temp,
      activityId: activityId,
      cover: cover         
    })
    //自动播放
    this.listenerButtonPlay();
                
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isOpen: true, //播放开关
      starttime: "00:00", //正在播放时长
      duration: "00:00", //总时长
      offset: 0,
      max: 0
    })
    innerAudioContext.stop();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.setData({
      isOpen: true, //播放开关
      starttime: "00:00", //正在播放时长
      duration: "00:00", //总时长
      offset: 0,
      max: 0
    })
    innerAudioContext.stop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  /**
   * 提交到作品上传页面
   */
  submitInfo: function(e){
    console.log(e);
    var that = this;
    storage.put('audio_objectid',that.data.objectid, 3600);
    storage.put('temp',that.data.src, 3600);
    wx.redirectTo({
      url: '/pages/zpsc/upload/upload?' + "&activityId=" + that.data.activityId + "&cover=" + that.data.cover,
      
    })
    
  }
});
